package com.yzl.frelink.entities.chart;

import java.util.ArrayList;
import java.util.List;

import com.yzl.frelink.entities.Fretish;

import lombok.Data;

@Data
public class StateTreeNode {
    String name; // name for state
    Fretish fretish;
            
    // actions for state 
    List<String> entry; 
    List<String> during;
    List<String> exit;
    
    // String inGuard;
    // String inEvent;
    // String outGuard;
    // String outEvent;

    private StateTreeNode parent;
    private List<StateTreeNode> children;

    private int[] position;
    private int type; // 0: normal state, 1: start, 2: junction, 3: parallel

    public static final int NORMAL = 0;
    public static final int START = 1;
    public static final int JUNCTION = 2;
    public static final int PARALLEL = 3;

    public StateTreeNode(String name, int type) {
        this.name = name;
        this.entry = new ArrayList<>();
        this.during = new ArrayList<>();
        this.exit = new ArrayList<>();
        this.type = type;
        this.children = new ArrayList<>();
        this.parent = null;
    }
    
    public void addChild(StateTreeNode child) {
        child.parent = this; // 设置父节点
        children.add(child);
    }
    
    public StateTreeNode getParent() {
        return parent;
    }

    public boolean hasChild(StateTreeNode child) {
        return children.contains(child);
    }

    public void addEntryAction(String action) {
        String[] entryAto = action.split("\n");
        for (String entry : entryAto) {
            this.entry.add(entry);
        }
    }

    public void addDuringAction(String action) {
        String[] duringAto = action.split("\n");
        for (String during : duringAto) {
            this.during.add(during);
        }
    }

    public void addExitAction(String action) {
        String[] exitAto = action.split("\n");
        for (String exit : exitAto) {
            this.exit.add(exit);
        }
    }

    public boolean hasAction() {
        return !(this.entry.isEmpty() && this.during.isEmpty() && this.exit.isEmpty());
    }

    public String getLabel() {
        String entry = String.join("\n", this.entry);
        String during = String.join("\n", this.during);
        String exit = String.join("\n", this.exit);
        return this.name + (entry.isEmpty() ? "" : ("\nen: " + entry)) 
                + (during.isEmpty() ? "" : ("\ndu: " + during)) 
                + (exit.isEmpty() ? "" : ("\nex: " + exit));
    }

    public String getMatlabLabel() {
        StringBuilder entry = new StringBuilder();
        for (int i = 0; i < this.entry.size(); i++) {
            String entryItem = this.entry.get(i);
            if (i == 0) entry.append("\"en: ");
            else entry.append("\"");
            entry.append(entryItem).append("\"");
            if (i < this.entry.size() - 1) entry.append("+newline+");
        }
        StringBuilder during = new StringBuilder();
        for (int i = 0; i < this.during.size(); i++) {
            String duringItem = this.during.get(i);
            if (i == 0) during.append("\"du: ");
            else during.append("\"");
            during.append(duringItem).append("\"");
            if (i < this.during.size() - 1) during.append("+newline+");
        }
        StringBuilder exit = new StringBuilder();
        for (int i = 0; i < this.exit.size(); i++) {
            String exitItem = this.exit.get(i);
            if (i == 0) exit.append("\"ex: ");
            else exit.append("\"");
            exit.append(exitItem).append("\"");
            if (i < this.exit.size() - 1) exit.append("+newline+");
        }
        return "\""+this.name+"\"" + (entry.isEmpty() ? "" : ("+newline+" + entry)) 
                + (during.isEmpty() ? "" : ("+newline+" + during)) 
                + (exit.isEmpty() ? "" : ("+newline+" + exit));
    }
}
